/**
 * 厦门大学计算机专业 | 前华为工程师
 * 专注《零基础学编程系列》http://lblbc.cn/blog
 * 包含：Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
 * 公众号：蓝不蓝编程
 **/
var app = getApp()
var interval = 0
var isStarted = false
Page({
  data: {
    startPauseButtonText: '开始',
    ball1: '15',
    ball2: '15',
    ball3: '15',
    ball4: '15',
    ball5: '15',
    ball6: '15',
    ball7: '15',
  },

  startOrPauseTimer() {
    if (isStarted) {
      if (interval != 0) {
        clearInterval(interval)
        interval = 0
      }
      this.setData({
        startPauseButtonText: '开始'
      })
    } else {
      this.setData({
        startPauseButtonText: '暂停'
      })

      if (interval == 0) {
        interval = setInterval(() => {
          this.setData({
            ball1: this.formatNumber(this.getRandom(33)),
            ball2: this.formatNumber(this.getRandom(33)),
            ball3: this.formatNumber(this.getRandom(33)),
            ball4: this.formatNumber(this.getRandom(33)),
            ball5: this.formatNumber(this.getRandom(33)),
            ball6: this.formatNumber(this.getRandom(33)),
            ball7: this.formatNumber(this.getRandom(16))
          })
        }, 100)
      }
    }

    isStarted = !isStarted
  },
  getRandom(maxNum) {
    return Math.floor(Math.random() * maxNum)+1
  },
  formatNumber(n) {
    n = n.toString()
    return n[1] ? n : `0${n}`
  }
})
